<?php 

function ListNewsletter()
{
    global $dbsql,$language,$id,$module;
    
    $sql_where  = " from tblnewsletter ";
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $countall   = $result['count'];
    list($lf,$la)= Slit_Page($countall);
    
    $sql_select = "select * ".$sql_where." order by iId desc limit $lf,$la";
    return $dbsql->load($sql_select);
}

function ListContentNewsLetterAdmin($id)
{
    global $dbsql,$language,$id,$module;
    
     $sql_where  = " from tblnewsletter_content where iParentId=$id ";
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $countall   = $result['count'];
    list($lf,$la)= Slit_Page($countall);
    
    $sql_select = "select * ".$sql_where." order by iOrder limit $lf,$la";
    return $dbsql->load($sql_select);
}

function SendNewsletter($idNews,$listidEmail=array())
{
    global $dbsql,$language,$root_domain;
    
    if(is_array($listidEmail) && count($listidEmail)>0)
    {
        $listIdStr  = implode(',',$listidEmail);   
    }
    else
    {
        return false;
    }
            
    list($subject,$message)  = LoadDataNewsletter($idNews);    
    
    if(empty($message))
    {
        echo 'load newsletter fail ';
        return false;
    }   
    //load email from list id:    
    $sql_select="select cEmail,cName,cCode from tblnewsletter_email where iId in($listIdStr)";
	$rs        =$dbsql->query($sql_select);       
        	
	while ( $result = $dbsql->fetch_array($rs) )
	{
        $email      = $result['cEmail'];
        $name       = $result['cName'];
        $code       = $result['cCode'];
		$rsmsend    = sendEmailNewsletter($email,$name,$code,$subject,$message,$idNews);
        if($rsmsend)
        {            
            echo '=>'.$email.'<br>';
        }else
        {
            echo 'can\'t send newsletter to: '.$email.'<br>';  
        }
	}
}

function LoadDataNewsletter($id)
{
    global $dbsql,$language,$root_domain;
    
    ImportClass('newsletter');
    
    $sql_select = "select * from tblnewsletter where iId=$id ";    
    $result     = $dbsql->query_fetch($sql_select);
    if($result==false)
    {
        return false;
    }
        
	$subject        = $result['cName'];
	$message        = BuildHTMLNewsLetter($result,true);
    if(empty($subject))$subject='News Letter from '.$root_domain;
    
 
    return array($subject,$message);
}

function sendEmailNewsletter($email,$name,$code,$subject,$msg,$idNews)
{
    global $root_domain;
    
    $email           = trim($email);	
                        
	$linkunsubscribe = $root_domain."/as/unsubscribe/".$code.'".htm';
    
	$msg             = str_replace("{LINKUNSUBCRIBE}",$linkunsubscribe,$msg);
    $msg             = str_replace("Guest",$name,$msg);      
    
    $rs              = UpdateEmailSent($email,$idNews);
    $rsmail          = false;
    if($rs)
    {
        $rsmail      = send_email($email, "",$subject,$msg);
        if($rsmail==false)
        {
            UpdateEmailSent($email,$idNews-1);
        }
    }       	
  
    if($rsmail)
    { 	    
        return true;             
    }else
    {
        return false;
    }
}

function UpdateEmailSent($email,$idnow)
{
    global $dbsql,$language;
    
    $sql_update="update tblnewsletter_email set iMonth='".$idnow."' where cEmail='".$email."'";
	return $dbsql->query($sql_update); 
    
}
?>